<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

// Beépített letöltés kezelő

class hotn_animelist_download
{
	function hotn_animelist_download()
	{
		RETURN TRUE;
	}
	
	// Project készítésnél letöltés hozzáadása
	function hotn_animelist_download_create($project_name)
	{
		return array(
			"manga" => 0,
			"anime" => 0,
			"subtitle" => 0
		);
	}
	
	// File hozzáadása
	function hotn_animelist_download_add($post)
	{
		global $sql, $ns;
		
		// Adatok elenőrzése	
		$error = FALSE;
		
		if ($post['project_id'] == "none")
		{
			$error[] = HOTN_ANIMELIST_ADMIN_FILE_ERROR_FILE_PROJECT;
		}
		
		if ($post['download_name'] == "")
		{
			$error[] = HOTN_ANIMELIST_ADMIN_FILE_ERROR_NO_DOWNLOAD_NAME;
		}
		
		if ($post['download_episode'] == "")
		{
			$error[] = HOTN_ANIMELIST_ADMIN_FILE_ERROR_NO_FILES_EPISODE;
		}
		
		if ($post['download_url'] == "none" && $post['download_link'] == "")
		{
			$error[] = HOTN_ANIMELIST_ADMIN_FILE_ERROR_NO_DOWNLOAD_URL;
		}
		
		if ($post['download_type'] == "none")
		{
			$error[] = HOTN_ANIMELIST_ADMIN_FILE_ERROR_FILES_TYPE;
		}
		
		if ($error !== FALSE)
		{
			$ns -> tablerender(HOTN_ANIMELIST_ADMIN_DEFAULT_ERROR_TITLE, implode("<br>",$error) );
			return $post;
		}
		else
		{
			
			// Project info lekérdezése
			$sql->db_Select("hotn_animelist_project","*","`project_id` = '".$post['project_id']."'");
			$project = $sql->db_Fetch();
			
			// project állapotának számlálása
			if ( ( ($post['download_type'] == "subtitle") || ($post['download_type'] == "manga") ))
			{
				if ( strpos($post['download_episode'],"-") )
				{
					// mivel van benne kötőjel, ezért megnézzük, hogy hány db-ot jelöl az a két szám!
					$insert = explode("-",$post['download_episode']);
					$episode1 = (int)$insert['0'];
					$episode2 = (int)$insert['1'];
					$download_episode = $episode2-$episode1+1;		
				}
				else 
				{
					// Nincs kötőjel, ezért egyel növeljük
					$download_episode = 1;
				}
				$sql -> db_Update("hotn_animelist_project","`project_files` = `project_files` + '".$download_episode."' WHERE `project_id` ='".$project['project_id']."'");
			}
			else
			{
				$download_episode = $post['download_episode'];
			}
			
			if ($post['download_link']<>""){
				$post['download_url'] = $post['download_link'];
			}else{
				// File átmásolása
					// Mappák ellenőrzése
					if (!is_dir(e_DOWNLOAD.$project['project_folder']))
					{
						@mkdir(e_DOWNLOAD.$project['project_folder']);
							
					}
					//@chmod(e_DOWNLOAD.$project['project_folder'], 0775);
					if (!is_dir(e_DOWNLOAD.$project['project_folder']."/".$post['download_type']))
					{
						@mkdir(e_DOWNLOAD.$project['project_folder']."/".$post['download_type']);
							
					}
					//@chmod(e_DOWNLOAD.$project['project_folder']."/".$post['download_type'], 0775);
						
					$source = e_DOWNLOAD.$post['download_url'];
					$dest = e_DOWNLOAD.$project['project_folder']."/".$post['download_type']."/".$post['download_url'];
						
					//@chmod($source, 0775);
					rename($source,$dest);
			}
			
			$insert = array(
				"download_name" => $post['download_name'],
				"download_url" => $post['download_url'],
				"download_author" => USERID,
				"download_filesize" => "".filesize(e_DOWNLOAD.$post['download_url']),
				"download_project" => $post['project_id'],
				"download_type" => $post['download_type'],
				"download_datestamp" => time(),
				"download_episode" => $download_episode,
				"download_category" => $post['category_id'],
				"download_news" => 0
			);
			
			$sql->db_Insert("hotn_animelist_download",$insert);
			
			
			
			return TRUE;
			
		}
		
	}
	
	// Bizonyos letöltések listázása
	function hotn_animelist_download_get($project_id = 0,$type="all")
	{
		global $sql;
		
		$where = "";
		if ($project_id<>0)
		{
			$where = "WHERE `project_id` = '".$project_id."'";
		}
		
		$sql->db_Select("hotn_animelist_project","*",$where." ORDER BY `project_name` ASC","nondefault");
		$project = $sql->db_getList();
		foreach ($project as $project)
		{
			$sql->db_Select("hotn_animelist_download as download, ".MPREFIX."user as user","download.*, user.user_name as download_author","user.user_id = download.download_author and download.download_project = ".$project['project_id']." ORDER BY download_type ASC, download_name ASC");
			$return[$project['project_name']." (".$project['project_type'].")"] = $sql->db_getList();
		}
		
		return $return;
	}

	// Letöltés listázása
	function hotn_animelist_download_get_all()
	{
		$download = $this->hotn_animelist_download_get();
		foreach ($download as $download_name=>$download)
		{
			foreach ($download as $download)
				$return[$download_name][$download['download_id']] = $download['download_name'];
		}
		
		return $return;
	}
	
	// Letöltés szerkesztésre lekérés
	function hotn_animelist_download_get_one($download_id)
	{
		global $sql;
		
		$sql->db_Select("hotn_animelist_download","*","`download_id` = '".$download_id."'");
		
		return $sql->db_Fetch();
	}
	
	// Letöltés szerkesztése
	function hotn_animelist_download_edit($update,$id)
	{
		global  $sql;

		$update = implode(", ",$update);
		$sql->db_Update("hotn_animelist_download",$update." WHERE `download_id` = '".$id."'");
	}
	
	// Letöltés törlése
	function hotn_animelist_download_delete_file($download_id,$delete_file)
	{
		global $sql;
		
		$sql->db_Select("hotn_animelist_download","*","`download_id` = '".$download_id."'");
		$download = $sql->db_Fetch();
		
		$sql->db_Select("hotn_animelist_project","*","`project_id` = '".$download['download_project']."'" );
		$project = $sql->db_Fetch();
		
		$sql->db_Delete("hotn_animelist_download","`download_id` = '".$download_id."'");
		
		if ($download['download_type'] == "anime" || $download['download_type'] == "manga")
			$sql->db_Update("hotn_animelist_project","`project_files` = `project_files` - '".$download['download_episode']."' WHERE `project_id` = '".$download['download_project']."'");
		
		if ($delete_file == "true")
		{
			@unlink(e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".$download['download_url']);
		}
		else
		{
			$dest = e_DOWNLOAD.basename($download['download_url']);
			$source = e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".$download['download_url'];
			rename($source,$dest);
		}
	}
	
	// Project törlése
	function hotn_animelist_download_delete_all_file($project_id)
	{
		global $sql;
		
		$sql->db_Select("hotn_animelist_project","*","`project_id` = '".$project_id."'" );
		$project = $sql->db_Fetch();
		
		$sql->db_Select("hotn_animelist_download","*","`download_project` = '".$project_id."'");
		$download = $sql->db_getList();
		
		foreach ($download as $download)
		{
			@unlink(e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".basename($download['download_url']));
		}
		
		$sql->db_Delete("hotn_animelist_download","`download_project` = '".$project_id."'");
	}
	
	// Darab szám lekérése
	function hotn_animelist_download_count($arg)
	{
		global $sql;
		return $sql->db_Count("hotn_animelist_project","(*)",$arg);
	}
	
	// Project adatok lekérdezése
	function hotn_animelist_download_get_project($arg,$limit)
	{
		global $sql;
		$sql->db_Select("hotn_animelist_project","*",$arg." ".$limit,"non-default",FALSE);
		return $sql->db_getList();
	}
	
	// Egy project lekérdezése
	function hotn_animelist_download_get_one_project($id)
	{
		$project = $this->hotn_animelist_download_get_project("WHERE project_id = ".$id,"");
		return $project[1];
	}
	
	// Letöltések kikérése
	function hotn_animelist_download_get_download($project_id,$type)
	{
		
		if ($type == "manga")
		{
			$where = "download_type = 'manga' and category.category_id = download.download_category";
			$table = " , ".MPREFIX."hotn_animelist_category as category";
			$rows = " , category.*";
		}else{
			$where = "download_type = '".$type."'";
		}
			
		
		global $sql;
		$sql->db_Select(
			"hotn_animelist_download as download, ".MPREFIX."user as user".$table,
			"download.*, user.user_name as download_author, user.user_id".$rows,
			" user.user_id = download.download_author and download.download_project = '".$project_id."' and 
				".$where."
			ORDER BY download_name ASC",
			"default",
			false
		);	
		
		if ($type == "manga")
		{
			while($row = $sql->db_Fetch())
			{
				$return[$row['category_id']][] = $row;
			}	
			return $return;
		}
		else
		{
			return $sql->db_getList();
		}
	}
	
	// Letöltési link
	function hotn_animelist_download_link($id,$httppos=false)
	{
		define("IMAGE_DOWNLOAD", (file_exists(THEME."images/download.png") ? THEME."images/download.png" : e_IMAGE."generic/".IMODE."/download.png"));
		
		return "<a href='".e_PLUGIN."hotn_animelist/download.php?".$id."' ".($httppos===0?"target='_blank'":"")."><img src='".IMAGE_DOWNLOAD."' alt='' style='border:0' /></a>";
	}
	
	// Letöltési állapot kiszámítása
	function hotn_animelist_download_episode($download_type,$download_episode)
	{
		if ( ($download_type == "subtitle") || ($download_type == "manga") )
		{
			if ( strpos($download_episode,"-") )
			{
				// mivel van benne kötőjel, ezért megnézzük, hogy hány db-ot jelöl az a két szám!
				$insert = explode("-",$post['download_episode']);
				$episode1 = (int)$insert['0'];
				$episode2 = (int)$insert['1'];
				$download_episode = $episode2-$episode1+1;		
			}
			else 
			{
				// Nincs kötőjel, ezért egyel növeljük
				$download_episode = 1;
			}
		}
		
		return $download_episode;
	}
}
?>